// Copyright [2020] <unknown>
#include <bits/stdc++.h>
using namespace std;
// #define ONLINE_JUDGE
#ifndef ONLINE_JUDGE
#define dbg(x...)                             \
    {                                         \
        cerr << "\033[32;1m" << #x << " -> "; \
        err(x);                               \
    }
void err() { cerr << "\033[39;0m" << endl; }
template <typename T, typename... A>
void err(T a, A... x) {
    cerr << a << ' ';
    err(x...);
}
#else
#define dbg(...)
#endif

#define sz(x) ((int)x.size())

typedef long long LL;

typedef unsigned long long ULL;
const int N = 5e5 + 50;
const int MAGIC = 19260817, MOD = 998244353;
int t, n;

int u[N], v[N];
int m;
bool vis[N];

struct Hash {
    ULL v;
    LL w;
    int id;
    vector<int> a;
    void push_back(int x) {
        a.push_back(x);
    }
    void calc() {
        v = 0;
        sort(a.begin(), a.end());
        for (int x : a)
            v = (v * MAGIC + x) % MOD;
    }
    void clear() {
        w = id = v = 0;
        a.clear();
    }
} col[N];

int main(int argc, char const *argv[]) {
    // code
    int t;
    scanf("%d", &t);
    for (int kk = 0; kk < t; ++kk) {
        scanf("%d%d", &n, &m);
        // memset(col, 0, sizeof(Hash) * (n + 1));
        memset(vis, 0, sizeof(vis[0]) * (n + 1));
        LL s = 0;
        for (int i = 1; i <= n; ++i) {
            col[i].clear();
            scanf("%lld", &col[i].w);
            col[i].id = i;
        }
        for (int i = 0; i < m; ++i) {
            int u, v;
            scanf("%d%d", &u, &v);
            vis[v] = true;
            col[v].push_back(u);
        }
        for (int i = 1; i <= n; ++i) {
            if (vis[col[i].id]) {
                s += col[i].w;
                col[i].calc();
            }
        }

        sort(col + 1, col + 1 + n, [](const Hash &a, const Hash &b) {
            return a.v < b.v;
        });

        // for (int i = 1; i <= n; ++i)
        //     dbg(i, col[i].w, col[i].v, vis[col[i].id]);

        for (int i = 1, j; i <= n; i = j + 1) {
            // LL t = 0;
            j = i;
            if (!vis[col[i].id]) continue;
            LL t = col[i].w;
            while (j + 1 <= n && col[j + 1].v == col[i].v) {
                ++j;
                if (vis[col[j].id])
                    t += col[j].w;
            }
            // dbg(i, j, s, t);
            s = __gcd(s, t);
        }
        // cout << s << endl;
        printf("%lld\n", s);
    }

    return 0;
}